package com.lkc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkc.domain.ResponseResult;
import com.lkc.domain.dto.AnswerDto;
import com.lkc.domain.entity.Exam;
import com.lkc.domain.entity.Question;
import com.lkc.domain.entity.QuestionExam;
import com.lkc.domain.entity.UserExam;
import com.lkc.mapper.ExamMapper;
import com.lkc.mapper.QuestionExamMapper;
import com.lkc.mapper.QuestionMapper;
import com.lkc.mapper.UserExamMapper;
import com.lkc.service.ExamService;
import com.lkc.service.QuestionService;
import com.lkc.utils.entity.QuestionUtils;
import com.lkc.utils.security.LoginUser;
import org.apache.tomcat.util.descriptor.web.WebXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Exam)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2022-12-25 19:48:02
 */
@Service("examService")
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService {

    @Resource
    private ExamMapper examMapper;

    @Resource
    private UserExamMapper userExamMapper;

    @Resource
    private QuestionExamMapper questionExamMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public ResponseResult getByUid() {
        LambdaQueryWrapper<UserExam> userExamWrapper = new LambdaQueryWrapper<>();
        userExamWrapper.eq(UserExam::getUserId, LoginUser.getLoginUserId());
        List<UserExam> userExams = userExamMapper.selectList(userExamWrapper);
        List<Long> eIds = userExams.stream().map(UserExam::getExamId).collect(Collectors.toList());
        if (eIds.size() <= 0) {
            return ResponseResult.okResult(null);
        }
        LambdaQueryWrapper<Exam> examWrapper = new LambdaQueryWrapper<>();
        examWrapper.in(Exam::getId, eIds);
        examWrapper.orderByDesc(Exam::getStartTime);
        List<Exam> exams = list(examWrapper);
        //设置试卷的状态
        exams = exams.stream()
                .map(exam -> exam.setStatus(QuestionUtils.getStatus(exam)))
                .collect(Collectors.toList());
        return ResponseResult.okResult(exams);
    }


}

